package club.pinea.service.impl;

import club.pinea.constants.GameConfigConstants;
import club.pinea.constants.RedisPrefix;
import club.pinea.model.PoolScore;
import club.pinea.model.Seat;
import club.pinea.service.RedisService;
import club.pinea.utils.RedisCache;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

import static club.pinea.constants.RedisPrefix.GAME_POOL_SCORE_PREFIX;
import static club.pinea.constants.RedisPrefix.ROOM_SEAT_PREFIX;

@Service("redisService")
public class RedisServiceImpl implements RedisService {

    @Resource
    private RedisCache redisCache;

    @Override
    public void saveToken(String id, String token) {
        redisCache.setCacheObject(id, token);
    }

    @Override
    public boolean validateUserToken(Integer userId, String token) {
        String vToken = redisCache.getCacheObject(userId.toString());
        return Objects.equals(token, vToken);
    }

    @Override
    public int[] addPoolScores(int score, Integer seatId, int room) {
        PoolScore poolScore = redisCache.lIndex(GAME_POOL_SCORE_PREFIX + room, seatId);

        poolScore.set_4K(poolScore.get_4K() >= GameConfigConstants.max_4K ? GameConfigConstants.max_4K : poolScore.get_4K() + 1);
        poolScore.set_5K(poolScore.get_5K() >= GameConfigConstants.max_5K ? GameConfigConstants.max_5K : poolScore.get_5K() + 1);
        poolScore.set_RS(poolScore.get_RS() >= GameConfigConstants.max_RS ? GameConfigConstants.max_RS : poolScore.get_RS() + 1);
        poolScore.set_SF(poolScore.get_SF() >= GameConfigConstants.max_SF ? GameConfigConstants.max_SF : poolScore.get_SF() + 1);
        redisCache.lSet(GAME_POOL_SCORE_PREFIX + room, seatId, poolScore);

        return new int[]{poolScore.get_4K(), poolScore.get_SF(), poolScore.get_RS(), poolScore.get_5K()};
    }

    @Override
    public int selPoolScore(String string, Integer seatId, int room) {
        PoolScore poolScore = redisCache.lIndex(GAME_POOL_SCORE_PREFIX + room, seatId);
        int result = 0;
        switch (string) {
            case "4K":
                result = poolScore.get_4K();
                poolScore.set_4K(GameConfigConstants.min_4K);
                break;
            case "5K":
                result = poolScore.get_5K();
                poolScore.set_5K(GameConfigConstants.min_5K);
                break;
            case "RS":
                result = poolScore.get_RS();
                poolScore.set_RS(GameConfigConstants.min_RS);
                break;
            case "SF":
                result = poolScore.get_SF();
                poolScore.set_SF(GameConfigConstants.min_SF);
                break;
        }
        redisCache.lSet(GAME_POOL_SCORE_PREFIX + room, seatId, poolScore);
        return result;
    }

    @Override
    public void addNotices(String str) {
        redisCache.lPush("notices", str);
    }

    @Override
    public Integer getSeatsInfo(String username, Integer seatId, int room) {
        Seat seat = redisCache.lIndex(ROOM_SEAT_PREFIX + room, seatId);
        if (Objects.equals(username, seat.getSeatUser()) && Objects.equals(seatId, seat.getSeatId())) {
            return seatId;
        }
        return null;
    }

}
